/* 
 * File:   StepData.cpp
 * Author: root
 * 
 * Created on October 30, 2015, 1:11 AM
 */

#include "StepData.h"

uint32 StepData::instanceCount;

//StepData::StepData() {
//}
//
//StepData::StepData(const StepData& orig) {
//    *this = orig;
//}
//
//StepData::StepData(const SensorData& paraSensorA, 
//        const SensorData& paraSensorB, const SensorData& paraSensorC)
//{
//    sensor[0] = paraSensorA;
//    sensor[1] = paraSensorB;
//    sensor[2] = paraSensorC;
//}
//
//StepData::~StepData() {
//}

StepData::StepData() {
    ++StepData::instanceCount;
}

StepData::StepData(const StepData& orig) {
    *this = orig;
    ++this->instanceCount;
}

StepData::StepData(const SensorData& paraSensorA, 
        const SensorData& paraSensorB, const SensorData& paraSensorC)
{
    sensor[0] = paraSensorA;
    sensor[1] = paraSensorB;
    sensor[2] = paraSensorC;
    ++this->instanceCount;
}

StepData::~StepData() {
    --this->instanceCount;
}


StepData& StepData::operator=(const StepData& orig)
{
    if ( this == &orig ) {
        return *this;
    } else {
        sensor[0] = orig.sensor[0];
        sensor[1] = orig.sensor[1];
        sensor[2] = orig.sensor[2];
    }
    return *this;
}

void StepData::set(SensorData const &paraSensorA, 
        SensorData const &paraSensorB, SensorData const &paraSensorC)
{
    sensor[0] = paraSensorA;
    sensor[1] = paraSensorB;
    sensor[2] = paraSensorC;
}

double StepData::getMinLevel()
{
    double diff[2] = {0, 0};
    diff[0] = sensor[1].accelerator - sensor[2].accelerator;
    diff[1] = sensor[1].accelerator - sensor[0].accelerator;

    if(diff[0] <= diff[1])
        diff[1] = diff[0];
    return diff[1];
}

int StepData::getLevel()  const
{
  double diff[2] = {0, 0};
  
  diff[0] = sensor[1].accelerator - sensor[2].accelerator;
  diff[1] = sensor[1].accelerator - sensor[0].accelerator;
  
  return (diff[0] + diff[1])/2;
}

//uint32 StepData::isValid() const
//{
//    return !(StepData::isTooNarrow() || StepData::isTooWide()); 
//}

int StepData::isValid() const
{
    int result = 0;

    if (!StepData::isTooNarrow())
    {
            result = (StepData::isTooWide() ^ 1) & 0xFF;
    }
    return result;
}


int StepData::isTooWide() const
{
    int result = 1;
    
    if (StepData::getWidth() <= 2000) {
        result = 0;
    }

    return result;
}

int StepData::isTooNarrow() const
{
    uint32 result = 0;
    result = StepData::getWidth();
    return ((result <= 0xB3) + (result >> 31)) & 0xFF;
}

int StepData::getWidth() const
{
    return (sensor[2].time - sensor[0].time);
}

int StepData::getTime() const
{
    return sensor[0].time;
}


